package com.ronghuanet.car.controller;

import com.ronghuanet.basic.util.LoginContext;
import com.ronghuanet.car.CarOprDto;
import com.ronghuanet.car.es.doc.CarDoc;
import com.ronghuanet.car.query.CarSearchQuery;
import com.ronghuanet.car.service.ICarSearchService;
import com.ronghuanet.car.service.ICarService;
import com.ronghuanet.car.domain.Car;
import com.ronghuanet.car.query.CarQuery;
import com.ronghuanet.basic.util.AjaxResult;
import com.ronghuanet.basic.util.PageList;
import com.ronghuanet.org.domain.Employee;
import com.ronghuanet.org.domain.Shop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/car")
public class CarController {
    @Autowired
    public ICarService carService;


    /**
     * 保存和修改公用的
     * @param car  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Car car){

        //设置营销人员和店铺信息
        Employee seller = LoginContext.getLoginAdmin();
        car.setSellerId(seller.getId());
        Shop shop = seller.getShop();
        car.setShopId(shop.getId());
        car.setShopname(shop.getName());
        try {
            if( car.getId()!=null)
                carService.update(car);
            else
                carService.add(car);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            carService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {
            Car car = carService.getById(id);
            return AjaxResult.me().setResult(car);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        try {
            List< Car> list = carService.getAll();
            return AjaxResult.me().setResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult json(@RequestBody CarQuery query, HttpServletRequest request)
    {
        String token = request.getHeader("token");
        System.out.println(token);
        try {
            PageList<Car> pageList = carService.queryPage(query);
            return AjaxResult.me().setResult(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }

    @PostMapping("/onsale")
    public AjaxResult onsale (@RequestBody List<Long> ids)
    {
        try {
            Employee admin = LoginContext.getLoginAdmin();
            carService.onsale(new CarOprDto(admin.getId(),admin.getUsername(),ids));
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("上架失败！"+e.getMessage());
        }
    }
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids)
    {
        try {
            Employee admin = LoginContext.getLoginAdmin();
            carService.offsale(new CarOprDto(admin.getId(),admin.getUsername(),ids));
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("下架失败！"+e.getMessage());
        }
    }

    //============以上都是管理员接口，下面的接口是用户调用===================//
    @Autowired
    private ICarSearchService carSearchService;
    @PostMapping("/list")
    public AjaxResult list(@RequestBody CarSearchQuery query){
        try {
            PageList<CarDoc> pageList = carSearchService.search(query);
            return AjaxResult.me().setResult(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询数据失败！"+e.getMessage());
        }
    }


}
